package com.cn.xact.ty_treasured_book;

import java.util.ArrayList;
import java.util.List;

import com.cn.xact.ty_treasured_book.config.TYConfigBase;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class TYApplicationState {

    private final String TAG = this.getClass().getSimpleName();
    private ArrayList<TYApplicationModule> mCurModules = new ArrayList<TYApplicationModule>();
    private Context mContext;
    private int mModuleNum = 0;
    private String mCurrentPhone = null;
    private static String[] mSupportedPhone = null;

    public TYApplicationState(Context ct) {
        mContext = ct;
        initModules();
        initAppStatus();
    }

    private void initModules() {
        SharedPreferences sp = mContext.getSharedPreferences(TYConfigBase.TY_APPLICATION_RECORD_NAME, Context.MODE_PRIVATE);
        ArrayList<String> allModules = new ArrayList<String>();
        ArrayList<String> curModules = new ArrayList<String>();
        String[] modules = null;
        String[] amodules = mContext.getResources().getStringArray(R.array.ty_application_moduels);

        String spModules = sp.getString(TYConfigBase.TY_APPLICATION_RECORD_ITEM_LIST, null);

        if (spModules == null) {
            if (TYConfigBase.DEBUG)
                Log.w(TAG, "There is no presaved moduels, reset as default");

            modules = amodules;
        } else {
            modules = spModules.split(TYConfigBase.TY_APPLICATION_RECORD_SEPARATOR);
        }

        for (String module : modules) {
            curModules.add(module);
        }

        for (String module : amodules) {
            allModules.add(module);
        }

        initModuelsList(allModules, curModules);
        updateModules(mCurModules);

        initAppStatus();
    }

    public String getCurPhone() {
        return mCurrentPhone;
    }

    private void initAppStatus() {
        SharedPreferences sp = mContext.getSharedPreferences(TYConfigBase.TY_APPLICATION_STATUS_SP, Context.MODE_PRIVATE);
        mCurrentPhone = sp.getString(TYConfigBase.TY_APPLICATION_STATUS_CURRENT_PHONE, null);
    }

    public void updateAppStatus(String preference, String value) {
        SharedPreferences sp = mContext.getSharedPreferences(TYConfigBase.TY_APPLICATION_STATUS_SP, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.clear();
        editor.putString(preference, value);
        editor.commit();
    }

    private void initModuelsList(ArrayList<String> allModules, ArrayList<String> curModuels) {

        int skipCount = 0;
        int parentSeq = 0;
        String[] actions = mContext.getResources().getStringArray(R.array.ty_application_moduels_action);
        String[] icons = mContext.getResources().getStringArray(R.array.ty_application_moduels_icon);
        TYApplicationModule parent = null;

        for (int i = 0; i < allModules.size(); i++) {

            String module = allModules.get(i);

            if (skipCount > 0) {
                TYApplicationModule tmp = new TYApplicationModule(module, 0, true, actions[i], icons[i]);
                if (curModuels.contains(module))
                    tmp.setEnable(true);
                else
                    tmp.setEnable(false);
                skipCount--;

                if (tmp.getSeq() == parentSeq) {
                    parent.addChild(tmp);
                    mCurModules.add(tmp);
                }

                if (skipCount == 0) {
                    parentSeq = 0;
                    parent = null;
                }
            } else {
                TYApplicationModule tmp = new TYApplicationModule(module, 0, false, actions[i], icons[i]);
                if (curModuels.contains(module))
                    tmp.setEnable(true);
                else
                    tmp.setEnable(false);

                if (tmp.hasChild()) {
                    skipCount = tmp.getChildNum();
                    parentSeq = tmp.getSeq();
                    parent = tmp;
                } else {
                    // L1 menu will never be displayed, thus it`s ignored on module count
                    mModuleNum++;
                }

                mCurModules.add(tmp);
            }
        }
    }

    public void updateModules(List<TYApplicationModule> modules) {
        String tmp = "";

        for (TYApplicationModule module:modules) {
            if (module.isEnable())
                tmp = tmp + module.getName() + TYConfigBase.TY_APPLICATION_RECORD_SEPARATOR;
        }

        SharedPreferences sp = mContext.getSharedPreferences(TYConfigBase.TY_APPLICATION_RECORD_NAME, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.clear();
        editor.putString(TYConfigBase.TY_APPLICATION_RECORD_ITEM_LIST, tmp);
        editor.putBoolean(TYConfigBase.TY_APPLICATION_RECORD_ITEM_HAS_RECORD, true);
        editor.commit();
    }

    public ArrayList<TYApplicationModule> getModuleList() {
        return mCurModules;
    }

    public int getModuleNum() {
        return mModuleNum;
    }

    public void removeModule(String name) {
        for (int i = 0; i < mCurModules.size(); i++) {
            TYApplicationModule module = mCurModules.get(i);
            if (module.getDisplayName().equals(name)) {
                module.setEnable(false);
                if (module.hasChild()) {
                    for (TYApplicationModule child :module.getChilds()) {
                        child.setEnable(false);
                    }
                }
                updateModules(mCurModules);
                break;
            }
        }
    }

    public void addModule(String name) {
        for (int i = 0; i < mCurModules.size(); i++) {
            TYApplicationModule module = mCurModules.get(i);
            if (module.getDisplayName().equals(name)) {
                module.setEnable(true);
                if (module.hasChild()) {
                    for (TYApplicationModule child :module.getChilds()) {
                        child.setEnable(true);
                    }
                }
                updateModules(mCurModules);
                break;
            }
        }
    }

    public static void updateSupportedPhones(String[] phones) {
        mSupportedPhone = phones;
    }

    public static String[] getSupportedPhones() {
        return mSupportedPhone;
    }
}
